package com.wires.lu.practice;


import java.util.concurrent.*;

/**
 * 线程池
 *继承Thread类
 * 实现Runable 方法
 * 实现Callable
 *
 * 第四种 获得/使用java多线程的方式 ，线程池
 */
public class MyThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService threadPool=new ThreadPoolExecutor(2,5,
                1L,TimeUnit.SECONDS,new LinkedBlockingDeque<>(3)
                ,Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardPolicy());
        try {
            //模拟10个用户来办理业务，每个用户就是一个来自外部的请求线程
            for (int i = 0; i < 10; i++) {
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "\t 办理业务");
                });
                //TimeUnit.MILLISECONDS.sleep(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }

    private static void extracted() {
        System.out.println(Runtime.getRuntime().availableProcessors());
        // 接口   辅助工具类
        //Array Arrays
        //Collction Collections
        //Executor Executors

        ExecutorService threadPool1 = Executors.newFixedThreadPool(5);//1池5个处理线程
        ExecutorService threadPool2 = Executors.newSingleThreadExecutor();//单线程
        ExecutorService threadPool = Executors.newCachedThreadPool();//缓存多线程

        try {
            //模拟10个用户来办理业务，每个用户就是一个来自外部的请求线程
            for (int i = 0; i < 100; i++) {
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "\t 办理业务");
                });
                //TimeUnit.MILLISECONDS.sleep(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }

}
